/*
 * Copyright 2021 Scorpiour
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package cc.bestroute.halloween;

import java.util.Random;


/**
 * @author Scorpiour
 * @since 2021-12-24
 */
public class Halloween {

    public static void main(String[] args) {
        try {
            PriorityTaskDispatcher worker = new PriorityTaskDispatcher();
            Assignment assignment = new Assignment() {
                @Override
                public void process(Object ...args){
                    Double d = Double.parseDouble(args[0].toString());
                    Long delay = Long.parseLong(args[1].toString()) * 100L;
                    System.out.println("Assignment is processing args: " + d);
                    try {
                        Thread.sleep(delay);
                    } catch (InterruptedException exc) {

                    }
                }
            };
            Random rand = new Random();
            for (int i =0; i < 100; i++) {

                int pri = rand.nextInt(Priority.values().length);
                Priority priority = Priority.fromValue(pri);
                double arg = rand.nextDouble();
                long delay = Math.abs(rand.nextInt()) % 10;
                worker.attachAssignment(assignment, priority, arg, delay);
            }
            worker.close();

        } catch (Exception e) {
            System.out.println("Global Exception Captured");
            System.out.println(e);
        }
        
    }
}